<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('quick Search Placeholder', { fields: t('xPotfp.quick Search Fields') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" >
            <template #columnPrepend>
                <el-table-column  type="expand">
                    <template #default="props">
                        <template v-if="props.row.good">
                            <el-table :data="props.row.good||[]" :border="false">
                                <el-table-column width="80"/>
                                <el-table-column label="箱号" >
                                    <template #default="scope">
                                        <span class="tcno" @click="handleTankClick(scope.row)">{{ scope.row.TCNO }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column label="货物" prop="GOODSID" />
                                <!-- <el-table-column label="操作" >
                                    <template #default="scope">
                                        <el-button class="table-operate" type="primary" size="small" style="padding:4px 5px" @click="handleGoodsAdd(scope.row)">
                                            <span><i class="fa fa-pencil icon" style="font-size: 14px; color: rgb(255, 255, 255);"></i></span>
                                        </el-button>
                                    </template>
                                </el-table-column> -->
                            </el-table>
                        </template>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm v-if="baTable.form.operate=='view'" />
        <PopupFormGoods v-if="baTable.form.operate=='add'" />
        <TCForm v-if="baTable.form.operate=='查看'"/>
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted } from 'vue'
import baTableClass from '/@/utils/baTable'
import { xPotsq,authAdmin,dEPARTMENT } from '/@/api/controllerUrls'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import { i18n } from '/@/lang/index'
import PopupForm from './popupForm.vue'
import PopupFormGoods from './popupFormGoods.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import createAxios from '/@/utils/axios'
import { Lock,Unlock } from '@element-plus/icons-vue'
import TCForm from "../xPot/popupForm.vue"
const { t } = useI18n()
const tableRef = ref()

const optButtons = defaultOptButtons(["edit"])
optButtons[0].click = (row:any)=>{
    baTable.form.items = row
    baTable.form.operate = 'add'
}
console.log(optButtons)

const baTable = new baTableClass(
    // new baTableApi(xPotfp, '/distribution/'),
    new baTableApi(xPotsq,'_good'),
    {
        pk: 'TKID',
        column: [
            { type: 'selection', align: 'center', operator: false },
            // { label: t('xPotfp.ID'), prop: 'ID', align: 'center', operator: false,show:false },
            { label: t('xPotsq.TKID'), prop: 'TKID', align: 'center' },
            { label: t('xPotsq.CORP'), prop: 'CORP', align: 'center' },
            { label: t('xPotsq.YWTYPE'), prop: 'YWTYPE', align: 'center',comSearchRender: 'select',
            replaceValue: { '进口': '进口', '出口': '出口','LBL':'LBL' } },
            { label: t('xStockin.DEPT'), prop: 'DEPTNAME', align: 'center',operator:false },
            { label: t('xStockin.DEPT'), prop: 'DEPT', align: 'center',show:false,comSearchRender: 'remoteSelect', remote: {
                pk:'DEPTID',
                field: 'DEPTNAME',
                remoteUrl:dEPARTMENT +'index',
                params: {},
            } },
            { label: t('xPotsq.YBH'), prop: 'YBH', align: 'center', show: false },
            { label: t('xPotsq.GOODSID'), prop: 'GOODSID', align: 'center', show: false },
            { label: t('xPotsq.GOODS'), prop: 'GOODS', align: 'center', show: false },
            { label: t('xPotsq.CONTYPE'), prop: 'codename', align: 'center',operator:false },
            { label: t('xPotsq.CONSUM'), prop: 'CONSUM', align: 'center', operator: 'RANGE',
                render: 'customTemplate', customTemplate: (row: any) => {
                    return `${row.sum||'0'}/${row.count}`
                }
            },

            { label: t('xPotsq.CONSIZE'), prop: 'CONSIZE', align: 'center', show: false  },
            { label: t('xPotsq.REMARK'), prop: 'REMARK', align: 'center' },
            { label: t('xPotsq.OP'), prop: 'OP', align: 'center', show: false ,comSearchRender: 'remoteSelect', remote: {
                pk: 'nickname',
                field: 'nickname',
                remoteUrl:authAdmin+'index',
                params: {},
            } },
            { label: t('xPotsq.OPTIME'), prop: 'OPTIME', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160, show: false },
            { label: t('operate'), align: 'center', width: 150, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined, ],
        defaultOrder: { prop: 'TKID', order: 'desc' },
    },
    {
        defaultItems: [],
    },
    {
        onTableDblclick:(table:any)=>{
            baTable.form.items = table.row
            baTable.form.operate = 'view'
            return false
        }
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    // baTable.form.operate = 'add'
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})

const handleGoodsAdd = (item:any)=>{
    baTable.form.items = item
    baTable.form.operate = 'add'
}

const handleTankClick = (item:any)=>{
    baTable.form.loading = true
    baTable.form.operate = '查看'
    createAxios({
        url:'/admin/XPot/edit?TCNO='+item.TCNO
    }).then((res:any)=>{
        console.log(item)
        baTable.form.loading = false
        baTable.form.items = res.data.row
    })
}



</script>

<script lang="ts">
import { defineComponent } from 'vue'
import { ElMessage } from 'element-plus'
export default defineComponent({
    name: 'xPotfp',
})
</script>

<style scoped lang="scss">
.tcno{
    cursor: pointer;
    &:hover{
        color: var(--el-color-primary);
    }
}

</style>
